﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using Cn.Edwon.Web.Support;
using Cn.Edwon.Biz;
using Cn.Edwon.Spring;
using Cn.Cellula.Model;
using System.Collections.Generic;
using System.Text;
using System.IO;
using Cn.Cellula.Model.Content;
using Cn.Cellula.Client;
using Cn.Edwon.Web.Script;
using Cn.Edwon.Web;
using System.Reflection;

namespace Cn.Cellula.Website.Ws {
    public partial class _FileAction : PageSupport {
        IBiz biz = BizFactory.Get<IBiz>();
        String ModelName = "";
        String ModelKey = "";
        protected void Page_Load(object sender, EventArgs e) {
            ModelName = Request.QueryString["model"];
            ModelKey = Request.QueryString["model_key"];
            String act = Request.QueryString["act"];
            if (act == "del") {
                DeleteFiles();
            } else if (act == "list") {
                LoadFiles();
            } else if (act == "top") {
                TopFile();
            } else {
                Response.Clear();
                Response.Write("[]");
                Response.End();
            }
        }
        private void DeleteFiles() {
            String keyStr = Request.QueryString["keys"];
            if (String.IsNullOrEmpty(keyStr)) {
                Response.Clear();
                Response.Write("[]");
                Response.End();
            } else {
                String[] keys = keyStr.Split(',');
                String bk = "[0";
                try {
                    foreach (String k in keys) {
                        TUploadFileEO eo = biz.Load<TUploadFileEO>(k);
                        if (eo.ModelName == ModelName && eo.ModelKey == ModelKey) {
                            if (File.Exists(ClientHelper.RootPath + eo.FilePath)) {
                                File.Delete(ClientHelper.RootPath + eo.FilePath);
                            }
                            if (File.Exists(ClientHelper.RootPath + eo.Thumbnail)) {
                                File.Delete(ClientHelper.RootPath + eo.Thumbnail);
                            }
                            biz.Delete<TUploadFileEO>(k);
                            bk += ",\"" + k + "\"";
                        }
                    }
                } catch(Exception ex) {
                    log.Error("删除文件失败:" + ex.Message);
                }
                bk += "]";
                Response.Clear();
                Response.Write(bk);
                Response.End();
            }
        }

        private void TopFile() {
            String key = Request.QueryString["key"];
            String bk = "";
            if (!String.IsNullOrEmpty(key)) {
                try {
                    TUploadFileEO eo = biz.Load<TUploadFileEO>(key);
                    eo.UploadDate = DateTime.Now;
                    if (eo.OwnerKey != this.OwnerKey) {
                        bk = "权限错误";
                    } else {
                        biz.Update<TUploadFileEO>(eo);
                    }
                    bk = "";
                } catch(Exception ex) {
                    bk = ex.Message;
                }
            }
            Response.Clear();
            Response.Write(bk);
            Response.End();
        }
        private void LoadFiles() {
            StringBuilder sb = new StringBuilder();
            try {
                String hql = "from TUploadFileEO eo where eo.ModelName='" + ModelName + "' and eo.ModelKey='" + ModelKey + "' order by eo.UploadDate desc ";
                IList<Object> list = biz.Find<Object>(hql);
                sb.Append(JsUtil.ParseEOList2Json(list));
            } catch {
                sb = new StringBuilder();
                sb.Append("null");
            }
            Response.Clear();
            Response.Write(sb.ToString());
            Response.End();
        }
    }
}
